package leetcode.lc206;

import leetcode.ListNode;

/**
 * <a href="https://leetcode.cn/problems/reverse-linked-list/">...</a>
 * 206. 反转链表
 *      递归解法
 *      1 -> 2 -> 3 -> 4  =>  4 -> 3 -> 2 -> 1
 *
 *      4 -> 3 -> 2 <-1
 *  宏观语义：反转以head为头的一个单向链表
 *      > 基条件，处理到最后一个节点，
 *
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode newHead = reverseList(head.next);
        if (newHead != null) { //
            head.next.next = head;
            head.next = null;
        }
        return newHead;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1,2,3,4);
        System.out.println(new Solution().reverseList(head));
    }
}